<?use pft\Prod\Product;
include 'common/autoload.php';
if(!$_SESSION['memberID']){exit("<script>window.location='dlogin.html'</script>");}
$ac=$GLOBALS['_ac'];
$pw=$GLOBALS['_pw'];
$s = new SoapClient(null,array("location" => "http://localhost/open/openService/pft_insideMX.php","uri" => "www.16u.com?ac_16u=$ac|pw_16u=$pw|auth_16u=true"));
$pid=$_REQUEST["pid"];
$did=$_REQUEST["did"];
$pri=$_REQUEST["pri"];
$fp=$s->get_Dynamic_Price_Merge($pid,date("Y-m-d"),2)/100;
$exist=$list=$has=$ch=$can=$temp=array();
if($did){
	$sel="select fid,pids from pft_product_sale_list where aid=".$_SESSION['sid']." and fid in (".implode(",",$did).")";
	$GLOBALS['le']->query($sel);
	while($row=$GLOBALS['le']->fetch_assoc()){
		if($row['pids']=="A"){$has[]=$row['fid'];}
		else{
			$t=array_flip(explode(",",$row['pids']));
			if(isset($t[$pid])){$has[]=$row['fid'];}
			$can[$row['fid']]=$t;
		}
	}
	$sel="select pid from uu_priceset where tid=".$pid." and pid in (".implode(",",$did).")";
	$GLOBALS['le']->query($sel);
	while($row=$GLOBALS['le']->fetch_assoc()){
		$exist[]=$row['pid'];
	}
}
$sel="select pft_rflex_id from uu_jq_ticket where pid=".$pid." limit 1";
$GLOBALS['le']->query($sel);
$GLOBALS['le']->fetch_assoc();
$rid=$GLOBALS['le']->f("pft_rflex_id");
foreach($did as $d){
	if($pri[$d]=="" || $pri[$d]=="禁售"){
		if(in_array($d,$has)){
			if(isset($can[$d])){unset($can[$d][$pid]);}
			else{
				if(!$list){
					$sel="select id from uu_products where p_status in (0,3) and apply_did=".$_SESSION['sid'];
					$GLOBALS['le']->query($sel);
					while($row=$GLOBALS['le']->fetch_assoc()){
						$list[]=$row['id'];
					}
					$list=array_flip($list);
				}
				$can[$d]=$list;
			}
			$ch[$d]=$can[$d];
		}
	}
	else{
		$p=$pri[$d]-$fp;
		if(filter_var($p, FILTER_VALIDATE_FLOAT)===false){
			exit("<script>alert('设定的价格存在非法字符!');</script>");
		}
		if($p<0){
			$sel="select dname from pft_member where id =".$d." limit 1";
			$GLOBALS['le']->query($sel);
			$GLOBALS['le']->fetch_assoc();
			exit("<script>alert('给".$GLOBALS['le']->f("dname")."配置的价格不正确');</script>");
		}
		if(in_array($d,$exist)){
			$temp[$d]=$p;
		}
		else{
			$str.="(".$d.",".$pid.",".($p*100)."),";
		}
		if(!in_array($d,$has)){
			$can[$d][$pid]=count($can[$d])+1;
			$ch[$d]=$can[$d];
		}
	}
}
//同步
if($rid){
	if($pri[4]=="" || $pri[4]=="禁售"){
		$pro = new Product($GLOBALS['le']);
		$ret = json_decode($pro->SyncTicketState(2, $pid),true);
		if($ret['status']!="ok"){
			exit("<script>alert('".$pid.$ret['msg']."');</script>");
		}
	}
	elseif(isset($pri[4])){
		$p=$pri[4]-$fp;
		$pro = new Product($GLOBALS['le']);
		$ret = json_decode($pro->SyncTicketState(1, $pid),true);
		if($ret['status']!="ok"){
			exit("<script>alert('".$pid.$ret['msg']."');</script>");
		}
		$price = new Price(-1);
		$ret = $price->PriceSync($pid, $rid, ($p*100));
		if($ret!=100){
			exit("<script>alert('".$pid."-".$rid."-".$p."失败".$ret."');</script>");
		}
	}
}
foreach($temp as $k=>$v){
	$upd="update uu_priceset set dprice=".($v*100)." where pid=".$k." and tid=".$pid." limit 1";//echo $upd;
	$GLOBALS['le']->query($upd);
}
foreach($ch as $k=>$v){
	$upd="update pft_product_sale_list set pids='".implode(",",array_unique(array_diff(array_flip($v), array(null,'null','',' '))))."' where aid=".$_SESSION['sid']." and fid=".$k;//echo $upd;
	$GLOBALS['le']->query($upd);
}
if($str){
	$ins="insert into uu_priceset (pid,tid,dprice) values ".trim($str,",");//echo $ins;
	$GLOBALS['le']->query($ins);
}
$sel="select p_name from uu_products where id=".$pid." limit 1";
$GLOBALS['le']->query($sel);
$GLOBALS['le']->fetch_assoc();
$ins="insert pft_d_operation_rec set fid=".$_SESSION['memberID'].",sid=".$_SESSION['sid'].",daction='对".$GLOBALS['le']->f("p_name")."的分销价格进行配置',rectime=now()";
$GLOBALS['le']->query($ins);
echo "<script>alert('配置成功')</script>";
?>